package com.qihunet.springbootjwt.config;

import java.util.List;
import java.util.stream.Collectors;

import com.qihunet.springbootjwt.persistent.entity.UserInfo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;



final class JwtUserFactory {

    private JwtUserFactory() {
    }

    static JwtUser create(UserInfo user, List<String> roles) {

        return new JwtUser(
                user.getId(),
                user.getDisplayUserId(),
                user.getUsername(),
                user.getPassword(),
                user.getUserType(),
                mapToGrantedAuthorities(roles),
                user.getIsEnabled()
        );
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(List<String> authorities) {
        return authorities.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
        
    }
}
